import Vue from 'vue'
import VueRouter from 'vue-router'
// import store from '../store'
import IndexView from "../views/IndexView.vue"

Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("../views/LoginView.vue"),
  },
  {
    path: '/',
    name: 'IndexView',
    component: IndexView,
    children: [{
      path: "/users",
      name: "users",
      component: () => import("../views/Users/ShowUsersView.vue"),
    }, {
      path: "/add",
      name: "add",
      component: () => import("../views/Users/AddUsers.vue"),
    }, {
      path: "/edit",
      name: "edit",
      component: () => import("../views/Users/EditView.vue"),
    }, {
      path: "/info",
      name: "info",
      component: () => import("../views/system/PersonInfoView.vue"),
    }, {
      path: "/goods",
      name: "goods",
      component: () => import("../views/Goods/GoodsView.vue"),
    }, {
      path: "/create",
      name: "create",
      component: () => import("../views/Goods/CreateGoodsView.vue"),
    }, {
      path: "/editgoods",
      name: "editgoods",
      component: () => import("../views/Goods/EditGoodsView.vue"),
    }, {
      path: "/menus",
      name: "menus",
      component: () => import("../views/system/MenusView.vue"),
    }, {
      path: "/permission",
      name: "permission",
      component: () => import("../views/Users/AddPermissionView.vue"),
    }      , {      path: "/control/orderform",
      name: "orders",
      component: () => import("../views/Console/OrdersView.vue"),
    }, {
      path: "/control/order",
      name: "goodconsole",
      component: () => import("../views/Console/GoodsViews.vue"),
    },
    {
      path: "/control/good",
      name: "controlgoods",
      component: () => import("../views/Console/GoodsConsole.vue"),
    },
    {
      path: "/settlement",
      name: "settlement",
      component: () => import("../views/Console/SettlementView.vue"),
    },    {
      path: "/Spec",
      name: "Spec",
      component: () => import("../views/Console/GoodsSpec.vue"),
    },{
      path: "/api/stores",
      name: "Stores",
      component: () => import("../views/Stores/StoresView.vue"),
    }, {
      path: "/api/user_info",
      name: "Seluser_info",
      component: () => import("../views/user_info/Seluser_info.vue"),
    },{
      path: "/Users/RoleDept",
      name: "RoleDept",
      component: () => import("../views/Users/RoleDept.vue"),
    }
    ]
    },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // 登录状态验证  
  const isAuth = sessionStorage.getItem("token") !== null

  if (!isAuth && to.path !== '/login') {
    next('/login')
  } else if (isAuth && to.path === '/login') {
    next('/')      // 已登录用户访问登录页时跳转首页  
  } else {
    next()
  }
})

export default router  